<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 * Description of SearchMgr
 *
 * @author ebsi
 */
class SearchMgr {
    //put your code here
    private $dbc;
    private $searchTerms;
    
    public function __construct($dbc = null,$searchTerms = null)
    {
	if (isset($dbc))
	{
            $this->dbc = $dbc;
	}
        if(isset($searchTerms))
        {
            $this->searchTerms = $searchTerms;
        }
    }
    
    public function getAllSearchTerms()
    {
        $searchTermsArr = Array();
        if(isset($this->searchTerms))
        {
            foreach (explode(',',$this->searchTerms) as $st) {
                $searchTermsArr[]=  trim($st);
            }
        }
        
        return $searchTermsArr;
    }
    
    public function searchProducts()
    {	
        $_result=array();
        $repository=BO::getProductBORepository();
        if(isset($this->searchTerms))
        {
            foreach ($this->getAllSearchTerms() as $st) 
            {
               $products = array();
               $query = "select * from product where longdesc like '%".$st."%'";
               $r = mysqli_query($this->dbc, $query);
		while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
		{
                    $productbo=$repository->getProductBO($row['id']);
		    $products[]=$productbo;
		}
                $_result[$st] = $products;
            }
        }        
	return $_result;
    }
	
}
